package tv.huan.media.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tv.huan.common.core.text.Convert;
import tv.huan.common.utils.DateUtils;
import tv.huan.media.domain.Album;
import tv.huan.media.mapper.AlbumMapper;
import tv.huan.media.mapper.ClassificationMapper;
import tv.huan.media.domain.Classification;
import tv.huan.media.service.IClassificationService;

/**
 * 分类Service业务层处理
 * 
 * @author admin
 * @date 2019-10-08
 */
@Service
public class ClassificationServiceImpl implements IClassificationService 
{
    @Autowired
    private ClassificationMapper classificationMapper;

    @Autowired
    private AlbumMapper albumMapper;

    /**
     * 查询分类
     * 
     * @param classId 分类ID
     * @return 分类
     */
    @Override
    public Classification selectClassificationById(Long classId)
    {
        return classificationMapper.selectClassificationById(classId);
    }

    /**
     * 查询分类列表
     * 
     * @param classification 分类
     * @return 分类
     */
    @Override
    public List<Classification> selectClassificationList(Classification classification)
    {
        return classificationMapper.selectClassificationList(classification);
    }

    /**
     * 查询所有分类
     * @return 分类集合
     */
    public List<Classification> selectClassificationListAll(){
        return classificationMapper.selectClassificationListAll();
    }

    /**
     * 新增分类
     * 
     * @param classification 分类
     * @return 结果
     */
    @Override
    public int insertClassification(Classification classification)
    {
        classification.setCreateTime(DateUtils.getNowDate());
        return classificationMapper.insertClassification(classification);
    }

    /**
     * 修改分类
     * 
     * @param classification 分类
     * @return 结果
     */
    @Override
    public int updateClassification(Classification classification)
    {
        classification.setUpdateTime(DateUtils.getNowDate());
        return classificationMapper.updateClassification(classification);
    }

    /**
     * 删除分类对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteClassificationByIds(String ids) throws Exception
    {
        Long[] idArr = Convert.toLongArray(ids);
        int count;
        Album album = new Album();
        for (Long id : idArr){
            album.setClassId(id);
            count = this.albumMapper.getClassificationOrAuthorizerUseingCount(album);
            if (count > 0){
                throw new Exception("分类被使用，无法删除");
            }
        }
        return classificationMapper.deleteClassificationByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除分类信息
     * 
     * @param classId 分类ID
     * @return 结果
     */
    public int deleteClassificationById(Long classId)
    {
        return classificationMapper.deleteClassificationById(classId);
    }
}
